<?php

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Backstage\BCommonController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Models\CommonModel;
use App\Models\BackstageModel;
use Illuminate\Http\Request;
use App\Libary\Captcha\Code;
use Illuminate\Support\Facades\Validator;

// require_once '../app/libary/captcha/Code.class.php';

class Admin_groupController extends BCommonController
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/backstage/admin_group/index';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // 管理员列表首页
    public function index(Request $request){
        $list = BackstageModel::getGroupList();
        // dd($list);
        return view('backstage/admin_group/index',['list'=>$list]);
    }
    // 新增管理员
    public function add(){
        // dd($class);
        $data = BackstageModel::permissionList();
        // dd($data);
        return view('backstage/admin_group/add',['data'=>$data]);
    }
    public function doadd(Request $request){
        // 验证
        if( $request->isMethod('POST') ){//表单请求是否是POST请求
            $info = $request->except('_token');// 请求数据中除了_token以外的值
            if( empty($info['role']) ){
                return response()->json(['status'=>'0','message'=>'請輸入用戶組名稱！']);
            }elseif(strlen($info['role'])<4 || strlen($info['role'])>20){
                return response()->json(['status'=>'0','message'=>'請輸入4-20個字符作為用戶組名稱！']);
            }
            $authority = array();
            foreach ($info as $key=>$value) {
                if(is_array($value)){
                    $authority[$key] = $value;
                }
            }
            $data = array(
                'role' => trim($info['role']),
                'desc'=>trim($info['desc']),
                'authority'=>serialize($authority),
                'time'=>time()
            );            
            $res = CommonModel::addData('admin_group',$data);
            if($res){
                return response()->json(['status'=>'1','message'=>'操作成功！','jumpUrl'=>'/backstage/admin_group/index']);
            }else{
                return response()->json(['status'=>'0','message'=>'操作失败！']);
            }
        }
    }
    // 修改管理员
    public function edit($id){
        $info = CommonModel::getOne('admin_group',['id'=>intval($id)]);
        if(!empty($info->authority)){
            $info->authority=unserialize($info->authority);
        }
        $data = BackstageModel::permissionList();
        return view('backstage/admin_group/edit',['info'=>$info,'data'=>$data]);
    }
    public function doedit(Request $request){
        // 验证
        if( $request->isMethod('POST') ){//表单请求是否是POST请求
            $info = $request->except('_token');// 请求数据中除了_token以外的值
            $id = intval($info['id']);
            if( empty($info['role']) ){
                return response()->json(['status'=>'0','message'=>'請輸入用戶組名稱！']);
            }elseif(strlen($info['role'])<4 || strlen($info['role'])>20){
                return response()->json(['status'=>'0','message'=>'請輸入4-20個字符作為用戶組名稱！']);
            }
            $authority = array();
            foreach ($info as $key=>$value) {
                if(is_array($value)){
                    $authority[$key] = $value;
                }
            }
            // dd($authority);
            $data = array(
                'role' => trim($info['role']),
                'desc'=>trim($info['desc']),
                'authority'=>serialize($authority),
            );            
            $res = CommonModel::updateData('admin_group',['id'=>$id],$data);
            if($res){
                return response()->json(['status'=>'1','message'=>'操作成功！','jumpUrl'=>'/backstage/admin_group/index']);
            }else{
                return response()->json(['status'=>'0','message'=>'操作失败！']);
            }
        }
    }

   



}
